package co.th.bjc.md.helper;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import co.th.bjc.md.common.ConnectDB;
import co.th.bjc.md.model.Department;

public class DepartmentHelper {
	
	private static Logger _log = Logger.getLogger(DepartmentHelper.class);
	
	public List<Department> findAllDepartment() throws Exception{
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<Department> listDepartment = new ArrayList<Department>(0);
		ConnectDB connectDB = null;
		try{
			String sql = "SELECT * FROM department ";
			_log.info("SQL : " + sql);
			
			connectDB = new ConnectDB();
			conn = connectDB.getConnection();
			stmt = conn.prepareStatement(sql);
			
			rs = stmt.executeQuery();
			while (rs.next()){
				listDepartment.add(this.setDepartment(rs));
			}
		}catch(Exception ex){
			_log.error("findAllDepartment --> " + ex.getMessage());
			ex.printStackTrace();
		}finally{
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			connectDB.close();
		}
		return listDepartment;
	}
	
	public Department findDepartmentByDepartmentCode(BigDecimal departmentCode) throws Exception{
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		Department department = null;
		ConnectDB connectDB = null;
		try{
			String sql = "SELECT * FROM department WHERE department_code = ?";
			_log.info("SQL : " + sql);
			
			connectDB = new ConnectDB();
			conn = connectDB.getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setBigDecimal(1, departmentCode);
			
			rs = stmt.executeQuery();
			if (rs.next()){
				department = this.setDepartment(rs);
			}
		}catch(Exception ex){
			_log.error("findDepartmentByDepartmentCode --> " + ex.toString());
			ex.printStackTrace();
		}finally{
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			connectDB.close();
		}
		return department;
	}
	
	public List<Department> findDepartmentByExpertises(BigDecimal expertises) throws Exception{
		Connection conn = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		List<Department> listDepartment = new ArrayList<Department>(0);
		ConnectDB connectDB = null;
		try{
			String sql = "SELECT * FROM department WHERE expertises = ?";
			_log.info("SQL : " + sql);
			
			connectDB = new ConnectDB();
			conn = connectDB.getConnection();
			stmt = conn.prepareStatement(sql);
			stmt.setBigDecimal(1, expertises);
			
			rs = stmt.executeQuery();
			while (rs.next()){
				listDepartment.add(this.setDepartment(rs));
			}
		}catch(Exception ex){
			_log.error("findDepartmentByExpertises --> " + ex.toString());
			ex.printStackTrace();
		}finally{
			if (rs != null) rs.close();
			if (stmt != null) stmt.close();
			connectDB.close();
		}
		return listDepartment;
	}
	
	private Department setDepartment(ResultSet rs) throws Exception{
		Department department = new Department();
		department.setDepartmentCode(rs.getBigDecimal("department_code"));
		department.setDepartmentNameTH(rs.getString("department_name_th"));
		department.setDepartmentNameEN(rs.getString("department_name_en"));
		department.setDepartmentDetail(rs.getString("department_detail"));
		department.setExpertises(new ExpertisesHelper().findExpertisesByExpertisesID(rs.getBigDecimal("expertises")));
		department.setCreateBy(rs.getString("create_by"));
		department.setCreateDate(rs.getDate("create_date"));
		department.setUpdateBy(rs.getString("update_by"));
		department.setUpdateDate(rs.getDate("update_date"));
		return department;
	}
}
